#include <iostream>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>

//json序列化
inline void jsontest1()
{
    const char* name = "云飞";
    int age = 18;
    int grades[] = {60,70,80};
    Json::Value val;
    val["姓名"] = name;
    val["年龄"] = age;
    for(auto& n:grades) val["成绩"].append(n);

    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> writes(swb.newStreamWriter());

    std::stringstream ss;
    writes->write(val,&ss);
    std::cout<<ss.str()<<std::endl;
}

//json反序列化
inline void jsontest2()
{
    std::string str = R"({"姓名":"小华","年龄":20,"成绩":[80,90,100]})";
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> reads(crb.newCharReader());

    Json::Value val;
    std::string errs;
    bool ok = reads->parse(str.c_str(),str.c_str()+str.size(),&val,&errs);
    if(ok == false)
    {
        std::cout<<"parse error: "<<errs<<std::endl;
        return;
    }
    std::cout<<val["姓名"].asCString()<<std::endl;
    std::cout<<val["年龄"].asInt()<<std::endl;

    
    for(int i = 0;i<val["成绩"].size();++i)
        std::cout<<val["成绩"][i]<<" ";
    std::cout<<"\n";
}

int main()
{
    //jsontest1();
    jsontest2();

    return 0;
}